package pl.register.android.course.dialog;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import pl.register.android.R;
import pl.register.android.ValidationActivity;
import pl.register.android.storage.external.CourseRegisterSDCardManager;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */
public class DatabaseCreatorDialogBuilder extends Builder {

    private ValidationActivity activity;

    private EditText etStartYear;

    public DatabaseCreatorDialogBuilder(ValidationActivity activity) {
        super(activity);
        this.activity = activity;

        onCreateContent(activity);
    }

    private void onCreateContent(Context ctx) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_course_creator_layout, null);
        etStartYear = (EditText) dialogLayout.findViewById(R.id.start_year_course_edit_text);
        setView(dialogLayout);
    }

    @Override
    public AlertDialog create() {
        setTitle("Nie odnaleziono bazy");
        setIcon(android.R.drawable.ic_dialog_alert);
        setMessage("Na karcie SD nie znaleziono bazy dynych w folderze aplikacji '" + CourseRegisterSDCardManager.APP_PATH
                + "'. Jeśli chcesz utworzyć nową, proszę w poniższe pole wpisać rok rozpoczęcia nowego kursu.");
        setNegativeButton(R.string.btn_name_finish, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                activity.finish();
            }
        });
        setPositiveButton(R.string.btn_name_create, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String startYearString = etStartYear.getText().toString();
                if (startYearString != null && !startYearString.equals("")) {
                    int startYear = Integer.parseInt(startYearString);
                    activity.createCourseFor(startYear);
                    dialogInterface.dismiss();
                } else {
                    activity.finish();
                }
            }
        });
        return super.create();
    }


}
